﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.SessionState;
using Kaiser.Infrastructure.User;
using Kaiser.Models;
using Kaiser.Models.JsonModels;
using Kaiser.Extensions;
using Kaiser.Services;

namespace Kaiser.Infrastructure.Controllers
{
    public class KaiserBaseController : Controller
    {
        public IUser CurrentUser
        {
            get
            {
                return HttpContext.Items[typeof(IUser).FullName] as IUser;
            }
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {

            base.Initialize(requestContext);
            ViewBag.CurrentUser = HttpContext.Items[typeof(IUser).FullName] as IUser;
            ViewBag.Site = new Site();


        }

        public new JsonResult Json(object data)
        {
            return base.Json(data, "text/html", JsonRequestBehavior.AllowGet);
        }

        public ActionResult Json(DataGrid result)
        {
            return Content(result.ToJson(), "text/html");
        }

        public ActionResult Json(JsonReturnMessages result)
        {
            return   Json(result, "text/html");
        }

        public void UpdateModel<TModel>(TModel model, FormCollection form) where TModel : class
        {
            base.UpdateModel<TModel>(model, form);
        }

        public ViewResult View(object model)
        {
           return base.View(model);
        }
    }

  

    public class KaiserBaseControllerHelp<T> where T : class
    {
        private readonly KaiserBaseController _controller;
        private   IServiceBase<T> _serviceBase;

        public IServiceBase<T>  Service
        {
            get { return _serviceBase; }
        }
        protected Action<T> AddAction;
        protected Action<T> EditAction;
        protected Action<IList<string>> DelAction;

        public KaiserBaseControllerHelp(KaiserBaseController controller, IServiceBase<T> serviceBase)
        {
            _controller = controller;
            _serviceBase = serviceBase;
        }

         public ActionResult GetModel(object primaryId)
         {
             var m = _serviceBase.Get(primaryId);
             return _controller.View(m);
         }
        
        public void UpdateModel(Object model, FormCollection form)
        {
           
        }

        public ActionResult AddModel( T model)
        {
            var result = new JsonReturnMessages();
            try
            {
                if (AddAction != null)
                {
                    AddAction(model);
                }
                else
                {
                    _serviceBase.Insert(model);
                }

                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return _controller.Json(result);
        }

        public ActionResult EditModel( object id, T model, FormCollection form)
        {
            var result = new JsonReturnMessages();
            try
            {
                if (EditAction != null)
                {
                    EditAction(model);
                }
                else
                {
                    var oldModel = _serviceBase.Get(id);

                    _controller.UpdateModel<T>(oldModel, form);
                    _serviceBase.Update(oldModel);
                }

                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return _controller.Json(result);
        }

        public ActionResult DelModel(  IList<string> ids)
        {
            var result = new JsonReturnMessages();
            try
            {
                if (EditAction != null)
                {
                    DelAction(ids);
                }
                else
                {
                    _serviceBase.DeleteList(ids);
                }

                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return _controller.Json(result);
        }
    }

    
}
